﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.IO;
using System.Data;
using SDD;
using NPOI.HSSF.UserModel;

namespace SDD
{
    class OptionDayLog
    {
        public static string dataDay = Program.DataDay;

        //public string url1 = "http://www.taifex.com.tw/chinese/3/7_12_1.asp";
        public string url1 = "http://www.taifex.com.tw/chinese/3/3_2_2_tbl.asp";

        private string FullFileName = Program.FileDir + "臺指選擇權行情表.xls";
        public string msg = "";

        /// <summary>
        /// 臺指選擇權行情表
        /// </summary>
        public void AggregateData()
        {
            // 期交所
            try
            {
                Debug.Write("dataDay:" + dataDay);
                analysisHTML(url1);
                new UpdateData().UpdateDataToGoogleDoc(FullFileName);
            }
            catch
            {
                Console.WriteLine("臺指選擇權行情表 資料擷取失敗!!");
            }
        }
        
        /// <summary>
        /// 解析HTML new
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ds"></param>
        public void analysisHTML(string url)
        {
            string context = new cHttp().dopost_UTF8(url);
            WebBrowser wbr = new WebBrowser();
            wbr.Navigate("");

            wbr.ScriptErrorsSuppressed = true;
            wbr.Document.Write("<script type='text/javascript'>window.onerror = function(){};</script>" + context);

            HtmlElementCollection tb = wbr.Document.GetElementsByTagName("TABLE");
            dataDay = wbr.Document.GetElementsByTagName("H3")[0].InnerHtml.Replace("日期：", "").Replace("/", "-");
            HtmlElementCollection tr = tb[0].GetElementsByTagName("TR");

            /////////////////////
            //建立工作表
            NPOIComFunction xls = new NPOIComFunction(FullFileName);
            if (xls.Workbook.GetSheetIndex(dataDay) > -1)
            {
                Console.WriteLine("臺指選擇權行情表 " + dataDay + " 資料已存在!!!");
                return;
            }
            HSSFSheet sheet = xls.Workbook.CreateSheet(dataDay);

            int r = 0;     // 要寫入excel的row順序

            /**臺指選擇權行情表**/
           for (int i = 0; i < tr.Count-2; i++)
            {
                for (int j = 0; j < tr[i].Children.Count; j++)
                {
                    if (sheet.GetRow(r + i) == null)
                        sheet.CreateRow(r + i);
                    if (sheet.GetRow(r + i).GetCell(j) == null)
                        sheet.CreateRow(r + i).CreateCell(j).SetCellValue(tr[i].Children[j].InnerText);
                    else
                        sheet.GetRow(r + i).GetCell(j).SetCellValue(tr[i].Children[j].InnerText);
                }
               // Console.WriteLine(r.ToString());
                //r++;
            }
           

            /****/
            //讓目前的工作表選為預設
            xls.Workbook.ActiveSheetIndex = xls.Workbook.NumberOfSheets - 1;
            xls.Workbook.SetSelectedTab(xls.Workbook.NumberOfSheets - 1);
            sheet.IsSelected = true;

            // 寫入實體檔案
            xls.WriteToFile();


        }
    }
}
